package by.wind.drebedengi.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Locale;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import by.wind.drebedengi.R;
import by.wind.drebedengi.ReportingActivity;
import by.wind.drebedengi.asynctask.AsyncTaskManager;
import by.wind.drebedengi.asynctask.DeleteAsyncTask;
import by.wind.drebedengi.datalayer.Constants;
import by.wind.drebedengi.datalayer.DeleteObject;
import by.wind.drebedengi.datalayer.bean.*;

public class RecordAdapter extends ArrayAdapter<Record>{
	
	// declaring our ArrayList of items
	private ArrayList<Record> objects;
	private Context itsContext;

	public RecordAdapter(Context context, int layoutResourceId, ArrayList<Record> objects) {
		super(context, layoutResourceId, objects);
		this.itsContext = context;
		this.objects = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		

		// assign the view we are converting to a local variable
		View v = convertView;
		
		// first check to see if the view is null. if so, we have to inflate it.
				// to inflate it basically means to render, or show, the view.
				if (v == null) {
					LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					v = inflater.inflate(R.layout.listview_item_row, null);
				}

				final Record record = objects.get(position);
				
				if (record != null) {

					// This is how you obtain a reference to the TextViews.
					// These TextViews are created in the XML files we defined.

					TextView lw_category = (TextView) v.findViewById(R.id.lw_category);
					TextView lw_sum = (TextView) v.findViewById(R.id.lw_sum);
					TextView lw_currency = (TextView) v.findViewById(R.id.lw_currency);
					TextView lw_date = (TextView) v.findViewById(R.id.lw_date);
					TextView lw_tag = (TextView) v.findViewById(R.id.lw_tag);

					SimpleDateFormat sdf = new SimpleDateFormat(Constants.DATE_PARSER_STRING,Locale.getDefault());
					
					final Button lw_edit = (Button) v.findViewById(R.id.lw_edit);
					final Button lw_delete = (Button) v.findViewById(R.id.lw_delete);
					
					lw_edit.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View view) {
							lw_edit.setVisibility(View.GONE);
							lw_delete.setVisibility(View.GONE);
						}
						
					});
					
					lw_delete.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View view) {
							AsyncTaskManager asyncTaskManager = ((ReportingActivity)itsContext).getAsyncTaskManager();
							asyncTaskManager.setupTask(new DeleteAsyncTask(itsContext, DeleteObject.WASTE, record));
							lw_edit.setVisibility(View.GONE);
							lw_delete.setVisibility(View.GONE);
						}
						
					});
					
					// check to see if each individual textview is null.
					// if not, assign some text!
					if (lw_category != null &&
							record.getCategory() != null){
						lw_category.setText(record.getCategory().getName());
					} else if (lw_category != null && record.getSource() != null) {
						lw_category.setText(record.getSource().getName());
					}
					if (lw_sum != null){
						lw_sum.setText(String.valueOf(Math.abs(record.getSum()) / Record.QUALIFICATION));
					}
					if (lw_currency != null &&
							record.getCurrency() != null){
						lw_currency.setText(record.getCurrency().getName());
					}
					if (lw_date != null){
						lw_date.setText(sdf.format(record.getOperation_date()));
					}
					if (lw_tag != null){
						lw_tag.setText(record.getComment());
					}
					
					//set the color of record
					if (record.getServer_id()>0)
						v.setBackgroundColor(Color.BLACK);
					else
						v.setBackgroundColor(Color.parseColor(itsContext.getString(R.string.color_row)));
				}

				// the view must be returned to our activity
				return v;
	}

	

}
